/*     */ package com.zimbra.cs.server;
/*     */ 
/*     */ import com.google.common.base.Charsets;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStream;
/*     */ import java.nio.charset.Charset;
/*     */ import org.apache.mina.core.buffer.IoBuffer;
/*     */ import org.apache.mina.core.future.WriteFuture;
/*     */ import org.apache.mina.core.session.IoSession;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class NioOutputStream
/*     */   extends OutputStream
/*     */ {
/*     */   private final IoSession session;
/*     */   private IoBuffer buf;
/*     */   private int maxScheduledBytes;
/*     */   private int maxWritePause;
/*     */   
/*     */   NioOutputStream(IoSession session, int chunkSize, int maxScheduleBytes, int maxWritePause)
/*     */   {
/*  37 */     this.session = session;
/*  38 */     this.buf = IoBuffer.allocate(chunkSize);
/*  39 */     this.maxScheduledBytes = maxScheduleBytes;
/*  40 */     this.maxWritePause = maxWritePause;
/*     */   }
/*     */   
/*     */   public synchronized void write(byte[] b, int off, int len) throws IOException
/*     */   {
/*  45 */     if ((off | len | b.length - (len + off) | off + len) < 0) {
/*  46 */       throw new IndexOutOfBoundsException();
/*     */     }
/*     */     
/*  49 */     if (len > this.buf.capacity()) {
/*  50 */       flush();
/*  51 */       writeToSession(IoBuffer.wrap(b, off, len));
/*     */     } else {
/*  53 */       if (len > this.buf.remaining()) {
/*  54 */         flush();
/*     */       }
/*  56 */       this.buf.put(b, off, len);
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void write(String s) throws IOException {
/*  61 */     int len = s.length();
/*     */     
/*  63 */     if (len > this.buf.capacity()) {
/*  64 */       flush();
/*  65 */       writeToSession(IoBuffer.allocate(len).putString(s, Charsets.UTF_8.newEncoder()).flip());
/*     */     } else {
/*  67 */       if (len > this.buf.remaining()) {
/*  68 */         flush();
/*     */       }
/*  70 */       this.buf.putString(s, Charsets.UTF_8.newEncoder());
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void write(int b) throws IOException
/*     */   {
/*  76 */     if (!this.buf.hasRemaining()) {
/*  77 */       flush();
/*     */     }
/*  79 */     this.buf.put((byte)b);
/*     */   }
/*     */   
/*     */   public synchronized void flush() throws IOException
/*     */   {
/*  84 */     if (this.buf.position() > 0) {
/*  85 */       this.buf.flip();
/*  86 */       writeToSession(this.buf);
/*  87 */       this.buf = IoBuffer.allocate(this.buf.capacity());
/*     */     }
/*     */   }
/*     */   
/*     */   private synchronized void writeToSession(Object output) throws IOException {
/*  92 */     long writeBytes = this.session.getScheduledWriteBytes();
/*  93 */     WriteFuture future = this.session.write(output);
/*  94 */     if (writeBytes > this.maxScheduledBytes) {
/*  95 */       ZimbraLog.nio.debug("IOSession has %d scheduled write bytes; waiting for buffer to catch up", new Object[] { Long.valueOf(writeBytes) });
/*  96 */       long start = System.currentTimeMillis();
/*  97 */       if (this.maxWritePause > 0) {
/*  98 */         boolean done = future.awaitUninterruptibly(this.maxWritePause);
/*  99 */         if (!done) {
/* 100 */           throw new IOException("Write stalled, client may have gone away");
/*     */         }
/*     */       } else {
/* 103 */         future.awaitUninterruptibly();
/*     */       }
/* 105 */       if (ZimbraLog.nio.isDebugEnabled()) {
/* 106 */         ZimbraLog.nio.debug("waited %d for %d scheduled bytes", new Object[] { Long.valueOf(System.currentTimeMillis() - start), Long.valueOf(writeBytes) });
/* 107 */         ZimbraLog.nio.debug("now have %d scheduled bytes, %d messages; %d written bytes %d messages", new Object[] { Long.valueOf(this.session.getScheduledWriteBytes()), Integer.valueOf(this.session.getScheduledWriteMessages()), Long.valueOf(this.session.getWrittenBytes()), Long.valueOf(this.session.getWrittenMessages()) });
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void close() throws IOException
/*     */   {
/* 114 */     flush();
/* 115 */     this.buf.free();
/* 116 */     this.session.close(false);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/server/NioOutputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */